//
//  AuctionAutoCell.swift
//  LeGou
//
//  Created by lxm on 2022/12/7.
//

import UIKit

class AuctionAutoCell: XMBaseTableViewCell {

    @IBOutlet weak var img: UIImageView!
    
    @IBOutlet weak var name: UILabel!
    
    @IBOutlet weak var subTitle: UILabel!
    
    @IBOutlet weak var bail: UILabel!
    
    @IBOutlet weak var startPrice: UILabel!
    
    @IBOutlet weak var sign: UILabel!
    
    @IBOutlet weak var selectBtn: UIButton!
    
    @IBOutlet weak var signBtn: UIView!
    
    @IBOutlet weak var btnLabel: UILabel!
    
    @IBOutlet weak var rightIcon: UIImageView!
    
    var onButton : (()->())?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        signBtn.zm.addTap(target: self, selector: #selector(onTap))
    }
    
    @objc func onTap() {
        if let onButton = onButton {
            onButton()
        }
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    func map(s : AuctionModels.AutoState?, interruptType : Int) -> Int {
        guard let s = s else {
            return -1
        }
        if s == .signed {
            return 0
        } else if s == .process {
            return 1
        } else if s == .stop {
            return interruptType == 0 ? 3 : 2
        } else if s == .over {
            return interruptType == 2 ? 5 : 4
        }
        return 0
    }
    
    func setStyle(s : Int) {
        if s == -1 {
            // 未报名
            signBtn.backgroundColor = Style.mainColor
            signBtn.layer.borderWidth = 0
            btnLabel.text = "立即报名"
            btnLabel.textColor = .white
            rightIcon.isHidden = false
        } else if s == 0 {
            // 已报名
            signBtn.backgroundColor = "#FCECE9".zm.hex()
            signBtn.layer.borderWidth = 0
            signBtn.layer.borderColor = Style.mainColor.cgColor
            btnLabel.text = "已报名"
            btnLabel.textColor = Style.mainColor
            rightIcon.isHidden = true
        } else if s == 1 {
            // 竞购中
            signBtn.backgroundColor = "#FCECE9".zm.hex()
            signBtn.layer.borderWidth = 0
            signBtn.layer.borderColor = Style.mainColor.cgColor
            btnLabel.text = "自动竞购中..."
            btnLabel.textColor = Style.mainColor
            rightIcon.isHidden = true
        } else if s == 2 {
            // 竞购中断
            signBtn.backgroundColor = .white
            signBtn.layer.borderWidth = 1
            signBtn.layer.borderColor = Style.mainColor.cgColor
            btnLabel.attributedText = "继续竞购".zm.append(str: "（参与次数不足，终止竞购）", attri: ["f" : "12m"])
            btnLabel.textColor = Style.mainColor
            rightIcon.isHidden = true
        } else if s == 3 {
            // 竞购中断
            signBtn.backgroundColor = .white
            signBtn.layer.borderWidth = 1
            signBtn.layer.borderColor = Style.mainColor.cgColor
            btnLabel.attributedText = "继续竞购".zm.append(str: "（保证金不足，终止竞购）", attri: ["f" : "12m"])
            btnLabel.textColor = Style.mainColor
            rightIcon.isHidden = true
        } else if s == 4 {
            signBtn.backgroundColor = "#CCCCCC".zm.hex()
            signBtn.layer.borderWidth = 0
            signBtn.layer.borderColor = Style.mainColor.cgColor
            btnLabel.text = "已结束"
            btnLabel.textColor = .white
            rightIcon.isHidden = true
        } else if s == 5 {
            signBtn.backgroundColor = "#CCCCCC".zm.hex()
            signBtn.layer.borderWidth = 0
            signBtn.layer.borderColor = Style.mainColor.cgColor
            btnLabel.text = "达到参与上限，终止竞购"
            btnLabel.textColor = .white
            rightIcon.isHidden = true
        }
    }
    
}
